﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FileDB;
using System.IO;

namespace FileDBTesting
{
    [TestClass]
    public class ReminderTest
    {
        #region
        [ClassInitialize]
        public static void ClassInitiaze(TestContext context)
        {
            DataManager.RemoveData();
            DataManager.Initiaze();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            
        }
        #endregion

        [TestMethod]
        public void Insert()
        {
            int seqId = Reminder.NextSeqID(0);
            var reminder = new Reminder()
            {
                CreatedTime = DateTime.Now,
                Message = "test",
                Category = ReminderCategory.GetReminder,
                Status = ReminderState.Actived,
                BatchId = Guid.NewGuid().ToString(),
                StartTime = DateTime.Now.AddDays(-2),
                DueTime = DateTime.Now.AddYears(1),
                ReminderTime = DateTime.Now.AddDays(16)

            };

            Reminder.Insert(reminder);

            var actualReminder = Reminder.GetReminder(Reminder.GetReminderFileName(reminder));

            Assert.IsTrue(AssertReminder(reminder, actualReminder));
            Assert.AreEqual(seqId + 1, Reminder.NextSeqID(0));
            Assert.AreEqual(reminder.Id +1, Reminder.NextID());
            Assert.AreEqual(reminder.FileName, Reminder.GetReminderFileName(reminder));

        }

        [TestMethod]
        public void Update()
        {
            int seqId = Reminder.NextSeqID(0);
            var reminder = new Reminder()
            {
                CreatedTime = DateTime.Now,
                Message = "test",
                Category = ReminderCategory.GetReminder,
                Status = ReminderState.Actived,
                BatchId = Guid.NewGuid().ToString(),
                StartTime = DateTime.Now.AddDays(-2),
                DueTime = DateTime.Now.AddYears(1),
                ReminderTime = DateTime.Now.AddDays(16)

            };

            Reminder.Insert(reminder);
            reminder.Message = "Update";
            Reminder.Update(reminder);
            var actualReminder = Reminder.GetReminder(Reminder.GetReminderFileName(reminder));
            Assert.IsTrue(AssertReminder(reminder, actualReminder));
            
            string fileName = reminder.FileName;
            reminder.Category  = ReminderCategory.RMap[ RCategory.PostReminder];
            Reminder.Update(reminder);
            Assert.IsTrue(Path.GetFileName(reminder.FileName).Contains(string.Format("-{0}-", (int)RCategory.PostReminder)));
            Assert.AreNotEqual(fileName, reminder.FileName);

            reminder.Status = ReminderState.Finished;
            fileName = reminder.FileName;
            Reminder.Update(reminder);
            Assert.IsTrue(Path.GetFileName( reminder.FileName).StartsWith(string.Format("{0}-", (int)RState.Finished)));
            Assert.AreNotEqual(fileName, reminder.FileName);
            Assert.AreEqual(Reminder.GetReminderFileName(reminder), reminder.FileName);

            Reminder.Delete(reminder);
            Assert.IsTrue(!File.Exists(reminder.FileName));
        }

        private bool AssertReminder(Reminder reminder, Reminder actualReminder)
        {
            if (reminder == null || actualReminder == null)
                return false;
            if (reminder.FileName != actualReminder.FileName)
                return false;


            if (reminder.Id != actualReminder.Id)
                return false;

            if (reminder.Message != actualReminder.Message)
                return false;

            if (reminder.Status != actualReminder.Status)
                return false;

            if (reminder.Category != actualReminder.Category)
                return false;

            if (reminder.BatchId != actualReminder.BatchId)
                return false;

            if (reminder.CreatedTime != actualReminder.CreatedTime)
                return false;

            if (reminder.DueTime != actualReminder.DueTime)
                return false;

            if (reminder.ReminderTime != actualReminder.ReminderTime)
                return false;

            if (reminder.StartTime != actualReminder.StartTime)
                return false;

            return true;
        }
    }
}
